//
//  BBFeatureCollectionViewController.swift
//  DSWeibo
//
//  Created by liuzhu on 2018/11/15.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

private let reuseIdentifier = "reuseIdentifier"//全局
class BBFeatureCollectionViewController: UICollectionViewController {

    private let pageCount = 4
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册一个cell
        collectionView.register(BBFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        layout.itemSize = UIScreen.main.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
        
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.isPagingEnabled = true
    }
    
    
    private var layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    //UICollectionViewController类指定的构造方法不是init（）而是init(collectionViewLayout:layout)，所以这里不用override
    init() {
        super.init(collectionViewLayout:layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: -UICollectionViewDataSource
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pageCount
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //获取cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as!BBFeatureCell
        //设置cell的数据
        //cell.backgroundColor = UIColor.red
        cell.imageIndex = indexPath.item
        // 返回cell
        return cell
    }

}

private class BBFeatureCell:UICollectionViewCell{
    
    var imageIndex:Int?{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
            if imageIndex == 3 {
                startButton.isHidden = false
                startButton.transform = CGAffineTransform(scaleX: 0.0,y: 0.0)
                startButton.isUserInteractionEnabled = false
                UIView.animate(withDuration: 3, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 2.0, options: UIView.AnimationOptions.init(rawValue: 0), animations: {
                    self.startButton.transform = CGAffineTransform.identity
                }) { (_) in
                     self.startButton.isUserInteractionEnabled = true
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func startBtnClick() {
       //去主页,切换根控制器一定要在appdelegate中切换
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: XMGSwitchRootViewControllerKey), object: true)
    }
    
    private func setupUI(){
        //添加子控件掉contentView上
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        //布局子控件的位置
        iconView.xmg_Fill(referView: contentView)
        startButton.xmg_AlignInner(type: XMG_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
    }
    
    private lazy var iconView = UIImageView()
    private lazy var startButton:UIButton = {
        let btn = UIButton()
        btn.isHidden = true
        btn.addTarget(self, action: #selector(startBtnClick), for: UIControl.Event.touchUpInside)
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), for: UIControl.State.normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), for: UIControl.State.highlighted)
        return btn;
    }()
}

//class FeatureLayout: UICollectionViewFlowLayout {
//    //准备布局
//    //先调用一共有多少行cell；然后调用这个方法准备布局；然后调用返回cell方法
//    override func prepare(){
//        //设置layout布局
//        layout.itemSize = UIScreen.main.bounds.size
//        layout.minimumLineSpacing = 0
//        layout.minimumInteritemSpacing = 0
//        layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
//
//        collectionView?.showsHorizontalScrollIndicator = false
//        collectionView?.bounces = false
//        collectionView?.isPagingEnabled = true
//    }
//}
